package cn.itcast.nems.order.service.impl;

import cn.itcast.nems.order.dao.entity.OrderDiscount;
import cn.itcast.nems.order.dao.mapper.OrderDiscountMapper;
import cn.itcast.nems.order.dto.OrderModeDiscountDTO;
import cn.itcast.nems.order.service.OrderDiscountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售订单-优惠 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2023-08-31
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrderDiscountServiceImpl extends ServiceImpl<OrderDiscountMapper, OrderDiscount> implements OrderDiscountService {
    private final OrderDiscountMapper orderDiscountMapper;

    @Override
    public List<Map<String, Object>> listOrderDiscount(String orderLineId) {
        return orderDiscountMapper.listOrderDiscount(orderLineId);
    }

    @Override
    public Integer getTotalAmount(String orderLineId) {
        return orderDiscountMapper.getTotalAmount(orderLineId);
    }

    @Override
    public List<OrderModeDiscountDTO> findDiscounts(String orderId) {
        final List<OrderDiscount> discounts = this.orderDiscountMapper.selectOrderDiscounts(orderId);
        return discounts
                .stream()
                .map(d -> new OrderModeDiscountDTO(d.getDiscountId(), d.getDiscountAmount(), d.getSort(), d.getDiscountScopeType(), d.getJournalDiscountLineId()))
                .collect(Collectors.toList());
    }
}
